import java.util.InputMismatchException;
import java.util.Scanner;

public class csz {
    public static void main(String[] args) {
        // write your code here
        //1.打印提示信息 欢迎来到猜数字
        System.out.println("欢迎来到猜数字");
        //2.生成数字
        int anser =  (int) (Math.random()*100+1);
        System.out.println(anser);
        //3.猜测数字
        Scanner num = new Scanner(System.in);
        while(true){
        int chances = 5;
        while(chances>0){
            //规定猜测次数
            System.out.println("请输入 1-100之间数字 "+"你还有"+chances+"机会");
            try{
                int guess =num.nextInt();
            }catch (InputMismatchException e){
                System.out.println("请输入数字");
                num.next();
                continue;
            }
            int guess = num.nextInt();
            //4.比较并完成
            if(guess==-1){
                break;

            } else if (guess<anser) {
                System.out.println("猜大了");

            }else if (guess>anser) {
                System.out.println("猜小了");
            }else
                System.out.println("猜对了");
                break;
            }

            chances = chances-1;

            System.out.println("再玩一次吗？（y/n)");
        String again = num.next();
        if(!again.equalsIgnoreCase("y")) {
            break;
        }


    }
}}
